//
//  NSString+MD5.m
//  LGHKitDemo
//
//  Created by yolanda on 2025/6/27.
//

#import "NSString+MD5.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (MD5)

+ (NSString *)gh_md5_32bit:(NSString *)inputString {
    const char *cStr = [inputString UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH]; // 16 字节
    CC_MD5(cStr, (CC_LONG)strlen(cStr), digest); // 计算哈希
    
    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [result appendFormat:@"%02x", digest[i]]; // 转为两位十六进制
    }
    return [result lowercaseString]; // 返回小写形式
}

+ (NSString *)gh_md5_16bit:(NSString *)inputString {
    NSString *md5_32 = [self gh_md5_32bit:inputString]; // 先获取32位结果
    if (md5_32.length >= 24) { // 验证有效性
        return [md5_32 substringWithRange:NSMakeRange(8, 16)]; // 截取中间16字符
    }
    return nil; // 无效输入返回空
}


@end
